import { getArticleAllRecommend, getArticleList, getArticleRecommend, getArchives, getArticleById, getArticleCategory } from "@/services";
import { IArticleItem, IRecommendedItem, IDetail } from "@/utils/interface";
import { makeAutoObservable } from "mobx"


class Article {
    constructor() {
        makeAutoObservable(this);
    }
    articleList: IArticleItem[] = []
    articleRecommend: IRecommendedItem[] = []
    articleDetail: IDetail = {}
    archives = []
    page = 1;
    total = 0;
    articleDetailList = []
    //  获取侧边栏推荐
    async getArticleRecommend() {
        // let result = await fetch('https://api.blog.wipi.tech/api/article/recommend').then(res=>res.json())
        let result = await getArticleRecommend();
        if (result.statusCode === 200) {
            this.articleRecommend = result.data
        }
    }
    // 获取顶部推荐
    async getArticleAllRecommend() {
        let result = await getArticleAllRecommend();

    }

    // 获取文章列表
    async getArticleList() {
        if (this.total && this.page * 12 >= this.total) {
            return;
        }
        let result = await getArticleList(this.page);
        if (result.statusCode === 200) {
            this.articleList = result.data[0]
            this.total = result.data[1];
            this.page++;
        }
    }

    // 获取归档列表
    async getArchives() {
        let result = await getArchives()
        if (result.statusCode === 200) {
            this.archives = result.data
        }
    }

    // 获取归档文件详情
    async getAirticleDetail(id: string) {
        let result = await getArticleById(id)
        if (result.statusCode === 200) {
            this.articleDetail = result.data
        }
    }
    // 点击文章分类
    async getArticleCategory(id: string, count: number) {
        let result = await getArticleCategory(id, count)
        console.log('点击文章分类--------', result.data[0]);
        if (result.statusCode === 200) {
            this.articleDetailList = result.data[0]
        }
    }
}

export default Article;